package job2;

import java.util.concurrent.SynchronousQueue;

/**
 * 同步队列,主线程消费,子线程生产
 */
public class Main12 {

    private static final SynchronousQueue<Integer> queue = new SynchronousQueue<>();

    public static void main(String[] args) throws Exception {
        long start = System.currentTimeMillis();

        // 异步执行 下面方法
        Thread thread = new Thread(() -> {
            try {
                queue.put(sum());
            } catch (InterruptedException e) {
            }
        });
        thread.start();

        Integer result = queue.take();
        // 确保  拿到result 并输出
        System.out.println("异步计算结果为：" + result);
        System.out.println("使用时间：" + (System.currentTimeMillis() - start) + " ms");
    }

    private static int sum() {
        return fibo(36);
    }

    private static int fibo(int a) {
        if (a < 2)
            return 1;
        return fibo(a - 1) + fibo(a - 2);
    }


}
